import CellType from '../celltypes';
import { WeaLocaleProvider } from "ecCom";
const getLabel = WeaLocaleProvider.getLabel;

export default {
    canUndo: true,
    execute: (context, options, isUndo) => {
        let { sheet, table, col: pos, colCount: addCols = 1, type } = options;
        try {
            sheet.suspendPaint();
            const { row: dataRow } = table.dataRange();
            if (isUndo) {
                const { name, row, col, rowCount, colCount, columns, cellTypes,pos,table } = options.old;
                sheet.deleteColumns(pos,1);
                sheet.tables.resize(table,rowCount, colCount);
                //sheet.tables.remove(name);
                //const oldTable = sheet.tables.add(name, row, col, rowCount, colCount, undefined);
                //oldTable.filterButtonVisible(false);
                // oldTable.bindColumns(columns.map(column => new GC.Spread.Sheets.Tables.TableColumn(column.dataField).name(column.name).dataField(column.dataField)));
                // oldTable.bindingPath(bindingPath);
                // cellTypes.forEach((cellType, index) => {
                //     sheet.getCell(dataRow, col + index).cellType(cellType);
                // });
                // labels.forEach((label, index) => {
                //     const tempCol = col + colCount + index;
                //     sheet.setValue(row, tempCol, label);
                // });
            } else {
                pos = pos + 1;
                const FieldCellType = CellType('FieldCellType');
                const tableJSON = table.toJSON();
                const { name, row, col, rowCount, colCount, columns } = tableJSON;
                const old = options.old = {};
                old.name = name;
                old.row = row;
                old.col = col;
                old.rowCount = rowCount;
                old.colCount = colCount;
                old.labels = [];
                old.pos = pos;
                old.table = table;
                //const fieldNames = old.cellTypes.filter(d => !!d).map(d => d.fieldName.toLowerCase());
                // for (let colIndex = 0; colIndex < addCols; colIndex++) {
                //     const tempCol = col + colIndex + colCount;
                //     old.cellTypes.push(sheet.getCell(dataRow, tempCol).cellType());
                //     old.labels.push(sheet.getValue(row, tempCol));
                // }
                //sheet.tables.remove(name);
                const newColCount = colCount + addCols;
                // const newTable = sheet.tables.add(name, row, col, rowCount, newColCount, undefined, { showHeader });
                // newTable.filterButtonVisible(false);
                const newColumns = [];
                let currentColIndex = col + newColCount - 1;
                let size = columns.length;
                sheet.addColumns(pos, addCols);
                sheet.tables.resize(table,rowCount, newColCount);
                //sheet.copyTo(Number(startRow), Number(curCol-1), Number(startRow), Number(curCol), rcount, 1, GC.Spread.Sheets.CopyToOptions.all);
                let num = -1;
                // 左边加取右边格式，右边加取左边格式
                if(type =="left") {
                    num = 1;
                }
                for(let i = 0 ; i < addCols ; i++) {

                    sheet.setColumnWidth(Number(pos+i), sheet.getColumnWidth(Number(pos+i)+num));
                }
                // for (let index = size - 1; index >= 0; index--) {
                //     const column = columns[index];
                //     if (index + col === pos) {
                //         let fieldIndex = 0;
                //         for (let colIndex = 0; colIndex < addCols; colIndex++) {
                //             let newField = `zd${++fieldIndex}`;
                //             while (fieldNames.indexOf(newField) > -1) {
                //                 newField = `zd${++fieldIndex}`;
                //             }
                //             fieldNames.push(newField);
                //             const label = `${getLabel('502711', '字段') + fieldIndex}`;
                //             newColumns.push(new GC.Spread.Sheets.Tables.TableColumn(newField).name(label).dataField(newField));
                //             const newCellType = new FieldCellType();
                //             newCellType.tableName = '' + sheet.parent.getActiveSheetIndex();
                //             newCellType.fieldName = newField;
                //             newCellType.fieldType = ['input', 'text', '200'];
                //             newCellType.detailIndex = bindingPath.replace(`${sheet.name()}.detail_`, '');
                //             newCellType.label = label;
                //             sheet.getCell(dataRow, currentColIndex).cellType(newCellType)
                //                 .textIndent(2.5)
                //                 .backgroundImageLayout(GC.Spread.Sheets.ImageLayout.none)
                //                 .backgroundImage(newCellType.getFieldTypeImage());
                //             currentColIndex--;
                //         }
                //     }
                //     newColumns.push(new GC.Spread.Sheets.Tables.TableColumn(column.dataField).name(column.name).dataField(column.dataField));
                //     const cellType = sheet.getCell(dataRow, col + index).cellType();
                //     sheet.getCell(dataRow, currentColIndex).cellType(cellType)
                //         .textIndent(2.5)
                //         .backgroundImageLayout(GC.Spread.Sheets.ImageLayout.none)
                //         .backgroundImage(cellType.getFieldTypeImage());
                //     currentColIndex--;

                //     // 最左边一行 左边加一行单独处理
                //     if (index === 0 && ((-1) + col) === pos) {
                //         let fieldIndex = 0;
                //         for (let colIndex = 0; colIndex < addCols; colIndex++) {
                //             let newField = `zd${++fieldIndex}`;
                //             while (fieldNames.indexOf(newField) > -1) {
                //                 newField = `zd${++fieldIndex}`;
                //             }
                //             fieldNames.push(newField);
                //             const label = `${getLabel('502711', '字段') + fieldIndex}`;
                //             newColumns.push(new GC.Spread.Sheets.Tables.TableColumn(newField).name(label).dataField(newField));
                //             const newCellType = new FieldCellType();
                //             newCellType.tableName = '' + sheet.parent.getActiveSheetIndex();
                //             newCellType.fieldName = newField;
                //             newCellType.fieldType = ['input', 'text', '200'];
                //             newCellType.detailIndex = bindingPath.replace(`${sheet.name()}.detail_`, '');
                //             newCellType.label = label;
                //             sheet.getCell(dataRow, currentColIndex).cellType(newCellType)
                //                 .textIndent(2.5)
                //                 .backgroundImageLayout(GC.Spread.Sheets.ImageLayout.none)
                //                 .backgroundImage(newCellType.getFieldTypeImage());
                //             currentColIndex--;
                //         }
                //     }
                // }
                // newTable.bindColumns(newColumns.reverse());
                // newTable.bindingPath(bindingPath);
            }
            sheet.resumePaint();
        } catch (e) {
            if (!!(window.console && window.console.log)) {
                window.console.log(e);
            }
        }
    }
};